package com.ultra.template.parse;


import com.ultra.enums.ArgumentTypeEnum;
import com.ultra.exception.UnableResolveTypeException;
import com.ultra.exception.UnknownTypeException;
import com.ultra.template.content.Argument;
import com.ultra.template.content.Pairs;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;


/**
 * @author yinger
 * @description TODO
 * @date 2024/12/30
 **/
public final class ArrayParser {
    /**
     * 从数组中创建对象数组，并填充对象数组
     *
     * @throws UnableResolveTypeException 无法解析类型异常
     * @throws UnknownTypeException       未知类型异常
     * @throws ClassNotFoundException     类未找到异常
     * @throws InvocationTargetException  调用目标异常
     * @throws NoSuchMethodException      无此方法异常
     * @throws InstantiationException     初始化异常
     * @throws IllegalAccessException     访问权限异常
     * @throws UnknownTypeException       未知类型异常`
     */
    public static void _fromArray(Argument argument) throws UnableResolveTypeException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, UnknownTypeException {
        argument.setIThCollection(Parser.times[argument.getDepth()]++);
        argument.setArgumentType(ArgumentTypeEnum.ARRAY);
        Type type = argument.getType();
        argument.setClassName(((Class<?>) type).getName());
        if (argument.getChild() == null || argument.getChild().length == 0) {
            argument.setChild(new Argument[]{new Argument()});
        }
        argument.getChild()[0].setType(((Class<?>) type).getComponentType());
        argument.getChild()[0].setDepth(argument.getDepth() + 1);
        argument.getChild()[0].setIThArgument(argument.getIThArgument());

        Parser.flatteningMap.put(new Pairs(argument.getIThArgument(),argument.getDepth(),argument.getIThCollection()),argument);
        Parser.parserDispatcher(argument.getChild()[0]);
    }
}